package com.flower.core_service.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.flower.core_service.object.entity.AuthUser;
import com.flower.core_service.service.AuthUserService;
import com.common.baseModel.LoginUser;
import com.common.baseModel.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;

/**
 * @ClassName LoginController
 * @Description TODO
 * @Author 吃麻椒的花酱
 * @Date 2022/2/26 22:58
 * @Version 1.0
 */
@RestController
@RequestMapping("/coreService/system/promise")
@Slf4j
@Api("登录处理")
public class LoginController {
    @Autowired
    AuthUserService authUserService;

    @ApiOperation("用户登录")
    @PostMapping("/login")
    public JsonResult<Object> login(@ApiParam(name="user",value = "用户登录信息",required = true) @RequestBody LoginUser user){
        QueryWrapper<AuthUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",user.getUsername());
        final AuthUser selectUser = authUserService.getOne(queryWrapper);
        if (selectUser == null) {
            JsonResult.createError("用户不存在!!!");
        }
        //String password = RSAUtils.decrypt(user.getPassword(), RSAUtils.SECRET);
       // final boolean eq = SecurityUtils.matchesPassword(password, selectUser.getPassword());
        //if (!eq) {
        //    ResponseResult.error().message("密码错误!!!");
       // }
        StpUtil.login(user.getUsername());
        log.info("登录成功");
        return JsonResult.create(StpUtil.getTokenValue());
    }

    @SentinelResource("info")
    //@SaCheckPermission("user-addddd")
    @GetMapping("/info")
    public JsonResult<LoginUser> info() {//72d7915d-7acf-414e-89a9-16d4f4a1efc4

        log.info("张阿花");

        LoginUser user=new LoginUser();
        ArrayList<String> role = new ArrayList<>();
        role.add("admin");
        user.setId(0);
        user.setName("flower");
        user.setUsername("张小花");
        user.setEmail("2558768850@qq.com");
        user.setPhone("9090950");
        user.setIntroduction("毕业设计");
        user.setAvatar("https://flowerbuckethua.oss-cn-hangzhou.aliyuncs.com/a.jpg");
        user.setPassword("11111");
        user.setRoles(role);
        return JsonResult.create(user);
    }

    @GetMapping("/logout")
    public JsonResult<Object> logout(String username) {

        StpUtil.logout(username);
        return JsonResult.isOk();
    }
}
